
import os
import config
import app.tools as tool
from PyQt4 import QtCore, QtGui


class Icon(object):

    _instance = None

    def __new__(cls, *args, **kw):
        if not cls._instance:
            cls._instance = super(Icon, cls).__new__(cls)
        return cls._instance

    def __init__(self):
        self.icons = set()
        self.pixmaps = set()
        icon_file_list = config.ICONS
        for icon_file in icon_file_list:
            filename = os.path.basename(icon_file)
            pixmap = self.load_pixmap(icon_file)
            self.pixmaps.add((filename, pixmap))
            self.icons.add((filename, self.load_icon(pixmap)))

    def load_pixmap(self, name):
        return QtGui.QPixmap(tool._from_utf8(tool.image(name)))

    def load_icon(self, pixmap):
        icon = QtGui.QIcon()
        icon.addPixmap(pixmap, QtGui.QIcon.Normal, QtGui.QIcon.Off)
        return icon

    def get(self, name):
        if not self.icons:
            return None
        for info in self.icons:
            filename, icon = info
            if filename == name:
                return icon
        return None

    def get_pixmap(self, name):
        if not self.icons:
            return None
        for info in self.pixmaps:
            filename, pixmap = info
            if filename == name:
                return pixmap
        return None
